#!/usr/bin/env python
#encoding:UTF-8
import urllib
import sys


import xml.dom.minidom

reload(sys)
sys.setdefaultencoding( "UTF-8" )


def parseWeb(url):
    data = urllib.urlopen(url).read().encode("UTF-8")
    dom = xml.dom.minidom.parseString(data)
    return parseDom(dom);

#解析xml的dom对象
def parseDom(dom):
    #得到文档元素对象
    root = dom.documentElement
    bookinfos = root.getElementsByTagName("i")
    booksnode = []

    for x in bookinfos:
        booksnode.append(x.childNodes[0].data)

    books = []
    for bookinfo in booksnode:
        bookinfoarry =  bookinfo.split('`')
        book = []
        book.append(bookinfoarry[1]) #名称
        book.append(bookinfoarry[0]) #id
        book.append(bookinfoarry[3]) #id
        book.append(bookinfoarry[4]) #id
        book.append(bookinfoarry[10]) #id
        books.append(book)
    return books


def parseNodes(nodes):
    books = []
    for bookinfo in nodes:
        bookinfoarry =  bookinfo.split('`')
        book = []
        book.append(bookinfoarry[1]) #名称
        book.append(bookinfoarry[0]) #id
        book.append(bookinfoarry[3]) #id
        book.append(bookinfoarry[4]) #id
        book.append(bookinfoarry[10]) #id
        books.append(book)
    return books


def doParse(url):
    books = parseWeb(url)
    return books

def getAllBook():
    params=urllib.urlencode({'n':24,'t':112,'d':0,'tag':0,'s':0,'a':0,'o':0})
    print params
    f=urllib.urlopen("http://app2.xingbook.com/r/ss.do",params)
    data = f.read().encode("UTF-8")
    dom = xml.dom.minidom.parseString(data)
    books= parseDom(dom);

    print len(books)




